//0元支付后调用 循环发送私信的接口
exports.sendMessage = function(req,res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;
    //最终结果
    var resultData = {};
    var o_name = "";
    var ovoucherNamepen = "";
    var open = [];
    var oid = 0;
    var o_money = 0;
    var o_type = 0;
    var o_aoc_id = 0;
    var templateType =  restfulParams.templateType
    //任务流创建
    async.waterfall([
        //查询订单信息
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_order+"/o_no/" + restfulParams.o_no,
                    method : 'POST',
                    data:{
                        fields:[
                            "o_oid", //机构id
                            "o_money", //订单金额
                            "o_type",//订单的类型 0-机构活动 1-论坛活动 2-课程\r\n
                            "o_aoc_id",
                            "o_name",//下单人的姓名
                        ].join(",")
                    }
                },
                function (err, data) {
                    console.log(data.data);
                    oid = data.data[0].o_oid;
                    o_money = data.data[0].o_money;
                    o_type = data.data[0].o_type;
                    o_aoc_id = data.data[0].o_aoc_id;
                    o_name = data.data[0].o_name;
                    cb();
                }
            );

        },
        //获取订单的课程或者机构活动名称
        function(cb) {
            if(o_type == 2){
                R.SEND_HTTP(
                    req, {
                        url: CORE_APIConfig.coreAPI.search_course + "/oc_id/"+ o_aoc_id,
                        method: 'POST',
                        data: {
                            fields:[
                                "oc_title",//课程名称
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        ovoucherNamepen = data.data[0].oc_title;
                        cb();
                    }
                );
            }else if(o_type == 0){
                R.SEND_HTTP(
                    req, {
                        url: CORE_APIConfig.coreAPI.search_organization_activity + "/oa_id/"+ o_aoc_id,
                        method: 'POST',
                        data: {
                            fields:[
                                "oa_title",//课程名称
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        ovoucherNamepen = data.data[0].oa_title;
                        cb();
                    }
                );
            }

        },
        //查询机构管理员的openid
        function(cb) {
            R.SEND_HTTP(
                req, {
                    url: CORE_APIConfig.coreAPI.searchOrganizationUser + "/ou_oid/"+ oid,
                    method: 'POST',
                    data: {
                        fields:[
                            "openid",//机构管理员的openid
                        ].join(",")
                    }
                },
                function (err, data) {
                    open = data.data;
                    cb();
                }
            );

        },
        //循环发送私信
        function (cb) {
            var tasks = [];
            open.forEach(function (item, i) {
                if(item.openid != null && item.openid != ''){
                	tasks.push(function (cb) {

                        R.SEND_HTTP(
                            req,
                            {
                                url: "http://127.0.0.1:3400/weixin/templateMessagesSent",
                                method: 'POST',
                                data  : {
                                    openid : item.openid,
                                    type   : templateType,
                                    userName:o_name, //学员名称
                                    voucherName:ovoucherNamepen, //课程名称
                                    voucherMoney:o_money, //订单金额
                                }
                            },
                            function (err, data) {

                                cb();

                            }
                        );

                    })
                }
            });
            async.waterfall(tasks, cb);
        }

    ],function(err, result){

        //返回最终拼接结果
        RES.response(res,true,"私信发送成功");

    });

}